package com.bitdf.txing.users.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.valid.SaveGroup;
import com.bitdf.txing.common.valid.UpdateGroup;
import com.bitdf.txing.users.entity.UserRoleEntity;
import com.bitdf.txing.users.service.UserRoleService;
import com.bitdf.txing.users.vo.UserRoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



/**
 * 
 *
 * @author lizhiwei
 * @email 
 * @date 2023-09-02 21:43:03
 */
@RestController
@RequestMapping("${txing.request.path.users}/role")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;

    /**
     * 分页查询
     */
    @SaCheckLogin
    @SaCheckPermission("user:role:list")
    @PostMapping("/list")
    public R list(@RequestBody UserRoleQueryVo queryVo){
        PageUtils page = userRoleService.queryPage(queryVo);
        return R.ok().put("data", page);
    }

    /**
     * 根据id获取
     */
    @SaCheckLogin
    @SaCheckPermission("user:role:info:get")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		UserRoleEntity userRole = userRoleService.getById(id);
        return R.ok().put("data", userRole);
    }

    /**
     * 新建角色
     */
    @SaCheckLogin
    @SaCheckPermission("user:role:save")
    @RequestMapping("/save")
    public R save(@Validated(SaveGroup.class) @RequestBody UserRoleEntity userRole, BindingResult result){
        List<String> permissionList = StpUtil.getPermissionList();
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
        boolean b = userRoleService.saveRole(userRole);
        return R.ok();
    }

    /**
     * 更新角色
     */
    @SaCheckLogin
    @SaCheckPermission("user:role:update")
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody UserRoleEntity userRole, BindingResult result){
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
		boolean b = userRoleService.updateRole(userRole);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION);
    }

    /**
     * 删除角色
     */
    @SaCheckLogin
    @SaCheckPermission("user:role:delete")
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		userRoleService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
